﻿using IOP.Extension.DependencyInjection;
using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;

namespace VkSample104
{
    public class InputSystem : ComponentSystem
    {
        [Autowired]
        private IGraphicsManager GraphicsManager { get; set; }
        private Camera Camera;
        private Ambient Ambient = new Ambient(0.1f, 0.1f, 0.1f, 0.1f);
        private float countE;
        private float spanE;
        private Vector4 u_clipPlane;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="contextManager"></param>
        public InputSystem()
        {
        }

        public override void Initialization()
        {
            GraphicsManager.TryGetCamera("main", out Camera camera);
            Camera = camera;
            ContextManager.Foreach<LightGroup>((group) =>
            {
                Context context = ContextManager.GetContext(group.Entity);
                var obj = context.GetContextRenderObject();
                var render = context.GetContextRenderGroup();
                PointLight point = obj.GetComponents<PointLight>().First();
                Span<Vector4> data = stackalloc Vector4[]
                {
                    new Vector4(Camera.EyePosition, 1),
                    point.Position,
                    Ambient,
                    point.Strength,
                    point.Strength,
                    u_clipPlane = new Vector4(1, -1, 1, 0)
                };
                Span<byte> p = MemoryMarshal.AsBytes(data);
                render.PushDataToTexture(p, 0, 0);
            });
        }

        public override void Update(TimeSpan timeSpan)
        {
            if(countE >= 2) spanE = -0.01f;
            else if (countE <= 0) spanE = 0.01f;
            countE += spanE;
            if (Input.GetMouseButtonState(MouseButton.ButtonLeft).Action == InputAction.Press)
            {
                var delta = Input.GetMousePositionDelta();
                if (delta.X != 0 || delta.Y != 0)
                {
                    Camera.EncircleFromMouse(Vector3.Zero, delta.X, delta.Y, (float)(0.2 * Math.PI / 180.0));
                }
            }
            ContextManager.Foreach<LightGroup>((group) =>
            {
                Context context = ContextManager.GetContext(group.Entity);
                var obj = context.GetContextRenderObject();
                var render = context.GetContextRenderGroup();
                PointLight point = obj.GetComponents<PointLight>().First();
                Span<Vector4> data = stackalloc Vector4[]
                {
                    new Vector4(Camera.EyePosition, 1),
                    point.Position,
                    Ambient,
                    point.Strength,
                    point.Strength,
                    u_clipPlane = new Vector4(1, countE - 1, -countE + 1, 0)
                };
                Span<byte> p = MemoryMarshal.AsBytes(data);
                render.PushDataToTexture(p, 0, 0);
            });
        }
    }
}
